package com.gui.juc.future;

import com.gui.utils.Sleeper;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CompletableFutureDemo3 {

    public static void main(String[] args) {

        List<String> resultList = new ArrayList<>();
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
            log.debug("开始执行");
            Integer num = null;
            if (num == 2) {
                log.debug("1");
            }
            Sleeper.sleep(2);
            return "Java";
        }, threadPool).thenAccept(result -> {
            resultList.add(result);
            log.debug("result:{}", result);
        }).exceptionally(e -> {
            log.error("完咯 发生异常咯:{}", e.getMessage());
            return null;
        });

        threadPool.shutdown();
        Sleeper.sleep(3);
        log.debug("执行结束:{}", resultList);
    }
}
